/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author Phạm Huy Đức
 */
public class thrSendEmailImages implements Runnable {

   public thrSendEmailImages() {
   }

   @Override
   public void run() {
      try {
         Thread.sleep(500);
      } catch (InterruptedException ex) {
         Logger.getLogger(thrProcessReportText.class.getName()).log(Level.SEVERE, null, ex);
      }
      System.out.println("Run thrSendEmailImages!");
      // Recipient's email ID needs to be mentioned.
      String to = "kataro92@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "timesheet@telsoft.com.vn";
      final String username = "timesheet@telsoft.com.vn";//change accordingly
      final String password = "timesheet123";//change accordingly

      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.socketFactory.class",
              "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "465");

      Session session = Session.getInstance(props,
              new javax.mail.Authenticator() {
         @Override
         protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
         }
      });

      try {

         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
                 InternetAddress.parse(to));

         // Set Subject: header field
         message.setSubject("Test gửi ảnh");

         // This mail has 2 part, the BODY and the embedded image
         MimeMultipart multipart = new MimeMultipart("related");

         // first part (the html)
         BodyPart messageBodyPart = new MimeBodyPart();
         String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
         messageBodyPart.setContent(htmlText, "text/html");
         // add it
         multipart.addBodyPart(messageBodyPart);

         // second part (the image)
         messageBodyPart = new MimeBodyPart();
         DataSource fds = new FileDataSource(
                 "C:\\Users\\Administrator.VIETFORUM\\Pictures\\chuyen-do-dau-ai-ngo.jpg");

         messageBodyPart.setDataHandler(new DataHandler(fds));
         messageBodyPart.setHeader("Content-ID", "<image>");

         // add image to the multipart
         multipart.addBodyPart(messageBodyPart);

         // put everything together
         message.setContent(multipart);
         // Send message
         Transport.send(message);

         System.out.println("Sent message successfully....");

      } catch (Exception ex) {
         Logger.getLogger(thrSendEmailImages.class.getName()).log(Level.SEVERE, null, ex);
      }
   }
}
